package com.xtourms.client.dep.hotel;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.xtourms.client.rm.hotel.HotelContant;
import com.xtourms.client.rm.hotel.HotelDetail;
import com.xtourms.client.rm.hotel.HotelManagerService;
import com.xtourms.client.rm.hotel.HotelManagerServiceAsync;
import com.xtourms.client.ui.util.LocationWidget;
import com.xtourms.client.ui.util.MessageBox;
import com.xtourms.client.ui.util.MoneyLabel;
import com.xtourms.client.ui.util.MoneyTextBox;
import com.xtourms.client.util.MysqlDateFormat;
import com.xtourms.client.util.Util;
import com.xtourms.shared.field.HotelField;

public class HotelDep extends Composite {

	private static HotelDepUiBinder uiBinder = GWT
			.create(HotelDepUiBinder.class);

	interface HotelDepUiBinder extends UiBinder<Widget, HotelDep> {
	}

	protected static HotelManagerServiceAsync hmsa = GWT.create(HotelManagerService.class);
	
	private JSONArray result;
	
	public HotelDep() {
		initWidget(uiBinder.createAndBindUi(this));
		result = new JSONArray();
		initSearchPanel();
		initResultTableHeader();
	}

	private void initResultTableHeader() {
		int col = 0;
		resultTable.getCellFormatter().setWidth(0, col, "100px");
		resultTable.setText(0, col++, "酒店名");
		resultTable.getCellFormatter().setWidth(0, col, "40px");
		resultTable.setText(0, col++, "等级");
		resultTable.getCellFormatter().setWidth(0, col, "60px");
		resultTable.setText(0, col++, "房型");
		resultTable.getCellFormatter().setWidth(0, col, "60px");
		resultTable.setText(0, col++, "早餐");
		resultTable.setText(0, col++, "每日价格");
		resultTable.setText(0, col++, "总价");
		resultTable.getRowFormatter().addStyleName(0, "result_header");
	}

	private void fill(JSONArray result){
		if(null != result && result.size() > 0){
			for(int index = 0; index < result.size(); index++){
				JSONObject ret = (JSONObject)result.get(index);
				
				if(0 == index){
					int col = 0;
					resultTable.getCellFormatter().setWidth(0, col, "100px");
					resultTable.setText(0, col++, "酒店名");
					resultTable.getCellFormatter().setWidth(0, col, "40px");
					resultTable.setText(0, col++, "等级");
					resultTable.getCellFormatter().setWidth(0, col, "60px");
					resultTable.setText(0, col++, "房型");
					resultTable.getCellFormatter().setWidth(0, col, "40px");
					resultTable.setText(0, col++, "早餐");
					JSONArray priceOfDay = (JSONArray)ret.get(HotelField.PRICE_OF_DAY);
					for(int i=0; i< priceOfDay.size(); i++){
						JSONObject dayPrice = (JSONObject)priceOfDay.get(i);
						String date = ((JSONString)dayPrice.get(HotelField.IN_DATE)).stringValue();
						resultTable.setText(0, col++, date);
					}
					resultTable.setText(0, col++, "总价");
					resultTable.getRowFormatter().addStyleName(0, "result_header");
				}
				fillRow(index+1, ret);
			}
		}else{
			initResultTableHeader();
		}
	}
	
	class detailShow implements ClickHandler {
		
		private int index;
		
		public detailShow(int index){
			this.index = index;
		}
		
		@Override
		public void onClick(ClickEvent event) {
			JSONObject ret = (JSONObject) result.get(index);
			JSONObject hotel = (JSONObject)ret.get(HotelField.HOTEL);
			HotelDetail detail = new HotelDetail(hotel);
			detail.center();
		}
	}
	
	private void fillRow(int row, JSONObject ret) {
		int col = 0;
		JSONObject hotel = (JSONObject)ret.get(HotelField.HOTEL);
		String name = ((JSONString)hotel.get(HotelField.HOTEL_NAME)).stringValue();
		Anchor hName = new Anchor(name);
		hName.addClickHandler(new detailShow(row-1));
		resultTable.setWidget(row, col++, hName);
		resultTable.setText(row, col++, ((JSONString)hotel.get(HotelField.HOTEL_LV)).stringValue());
		resultTable.setText(row, col++, ((JSONString)ret.get(HotelField.ROOM_STYLE)).stringValue());
		resultTable.setText(row, col++, ((JSONString)ret.get(HotelField.ROOM_BREAKFAST)).stringValue());
		JSONArray priceOfDay = (JSONArray)ret.get(HotelField.PRICE_OF_DAY);
		for(int i=0; i< priceOfDay.size(); i++){
			JSONObject dayPrice = (JSONObject)priceOfDay.get(i);
			String money = ((JSONNumber)dayPrice.get(HotelField.ROOM_SELL_PRICE)).toString();
			resultTable.setWidget(row, col++, new MoneyLabel(money));
		}
		String sum_money = ((JSONNumber)ret.get(HotelField.SUM_PRICE)).toString();
		resultTable.setWidget(row, col++, new MoneyLabel(sum_money));
		if(0 == row%2){
			resultTable.getRowFormatter().addStyleName(row, "odd");
		}else{
			resultTable.getRowFormatter().addStyleName(row, "even");
		}
	}
	
	@UiField FlexTable resultTable;
	
	@UiField TextBox hotel_name;
	@UiField LocationWidget location;
	@UiField ListBox hotel_lv;
	@UiField TextBox hotel_add;
	
	@UiField ListBox room_style;
	@UiField MoneyTextBox low_price;
	@UiField MoneyTextBox high_price;
	@UiField DateBox in_date;
	@UiField ListBox stay_day;
	
	private void initSearchPanel() {
		hotel_lv.addItem("不限");
		Util.fillListBox(hotel_lv, HotelContant.LV_STRS);
		room_style.addItem("不限");
		Util.fillListBox(room_style, HotelContant.ROOM_STYLE_STRS);
		in_date.setFormat(new MysqlDateFormat());
		in_date.setValue(new Date());
		for(int i = 1; i < 8; i++){
			stay_day.addItem(String.valueOf(i));
		}
	}
	
	@UiHandler("search")
	void search(ClickEvent evt){
		JSONObject condition = new JSONObject();
		condition.put(HotelField.HOTEL_NAME, new JSONString(hotel_name.getValue()));
		condition.put(HotelField.CITY_ID, new JSONNumber(location.getSelectedCityId()));
		condition.put(HotelField.HOTEL_ADD, new JSONString(hotel_add.getValue()));
		if(0 != hotel_lv.getSelectedIndex()){
			condition.put(HotelField.HOTEL_LV, new JSONString(hotel_lv.getItemText(hotel_lv.getSelectedIndex())));
		}
		if(0 != room_style.getSelectedIndex()){
			condition.put(HotelField.ROOM_STYLE, new JSONString(room_style.getItemText(room_style.getSelectedIndex())));
		}
		if(-1!=low_price.getMoney()){
			condition.put(HotelField.ROOM_LOW_PRICE, new JSONNumber(low_price.getMoney()));
		}
		if(-1!=high_price.getMoney()){
			condition.put(HotelField.ROOM_HIGH_PRICE, new JSONNumber(high_price.getMoney()));
		}
		//TODO 检查早于今天的日期
		condition.put(HotelField.IN_DATE, new JSONString(in_date.getValue().toString()));		
		condition.put(HotelField.STAY_DAYS, new JSONNumber(stay_day.getSelectedIndex()+1));
		
		hmsa.queryResult(condition.toString(), new AsyncCallback<String>() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onSuccess(String rooms) {
				resultTable.removeAllRows();
				JSONArray retArray = (JSONArray) JSONParser.parse(rooms);
				result = retArray;
				fill(retArray);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.ins.showError("查询酒店房间发生异常:"+caught.getMessage());
			}
		});
		return;
	}
	
	@UiHandler("reset")
	void reset(ClickEvent evt){
		hotel_name.setValue("");
		location.reset();
		hotel_lv.setSelectedIndex(0);
		hotel_add.setValue("");
		room_style.setSelectedIndex(0);
		low_price.setValue("");
		high_price.setValue("");
		in_date.setValue(new Date());
		stay_day.setSelectedIndex(0);
		return;
	}
}
